<template><div><p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="notifications" tabindex="-1"><a class="header-anchor" href="#notifications"><span>Notifications</span></a></h2>
<ul>
<li><a href="#introduction">介绍</a></li>
<li><a href="#generating-notifications">生成通知</a></li>
<li><a href="#sending-notifications">发送通知</a>
<ul>
<li><a href="#using-the-notifiable-trait">使用可通知的特征</a></li>
<li><a href="#using-the-notification-facade">使用通知门面</a></li>
<li><a href="#specifying-delivery-channels">指定传送渠道</a></li>
<li><a href="#queueing-notifications">通知队列化</a></li>
<li><a href="#on-demand-notifications">按需通知</a></li>
</ul>
</li>
<li><a href="#mail-notifications">邮件通知</a>
<ul>
<li><a href="#formatting-mail-messages">格式化邮件消息</a></li>
<li><a href="#customizing-the-sender">自定义发送者</a></li>
<li><a href="#customizing-the-recipient">自定义收件人</a></li>
<li><a href="#customizing-the-subject">自定义主题</a></li>
<li><a href="#customizing-the-mailer">自定义邮件程序</a></li>
<li><a href="#customizing-the-templates">自定义模板</a></li>
<li><a href="#mail-attachments">附件</a></li>
<li><a href="#adding-tags-metadata">添加标签和元数据</a></li>
<li><a href="#customizing-the-symfony-message">自定义 Symfony 消息</a></li>
<li><a href="#using-mailables">使用 Mailables</a></li>
<li><a href="#previewing-mail-notifications">预览邮件通知</a></li>
</ul>
</li>
<li><a href="#markdown-mail-notifications">Markdown 邮件通知</a>
<ul>
<li><a href="#generating-the-message">生成消息</a></li>
<li><a href="#writing-the-message">撰写消息</a></li>
<li><a href="#customizing-the-components">自定义组件</a></li>
</ul>
</li>
<li><a href="#database-notifications">数据库通知</a>
<ul>
<li><a href="#database-prerequisites">先决条件</a></li>
<li><a href="#formatting-database-notifications">格式化数据库通知</a></li>
<li><a href="#accessing-the-notifications">访问通知</a></li>
<li><a href="#marking-notifications-as-read">将通知标记为已读</a></li>
</ul>
</li>
<li><a href="#broadcast-notifications">广播通知</a>
<ul>
<li><a href="#broadcast-prerequisites">先决条件</a></li>
<li><a href="#formatting-broadcast-notifications">格式化广播通知的格式</a></li>
<li><a href="#listening-for-notifications">监听通知</a></li>
</ul>
</li>
<li><a href="#sms-notifications">短信通知</a>
<ul>
<li><a href="#sms-prerequisites">先决条件</a></li>
<li><a href="#formatting-sms-notifications">格式化短信通知格式</a></li>
<li><a href="#unicode-content">统一码内容</a></li>
<li><a href="#customizing-the-from-number">自定义「发件人」号码</a></li>
<li><a href="#adding-a-client-reference">添加客户参考</a></li>
<li><a href="#routing-sms-notifications">路由短信通知</a></li>
</ul>
</li>
<li><a href="#slack-notifications">Slack 通知</a>
<ul>
<li><a href="#slack-prerequisites">先决条件</a></li>
<li><a href="#formatting-slack-notifications">格式化 Slack 通知的格式</a></li>
<li><a href="#slack-interactivity">Slack 互动</a></li>
<li><a href="#routing-slack-notifications">路由 Slack 通知</a></li>
<li><a href="#notifying-external-slack-workspaces">通知外部 Slack 工作区</a></li>
</ul>
</li>
<li><a href="#localizing-notifications">本地化通知</a></li>
<li><a href="#testing">测试</a></li>
<li><a href="#notification-events">通知事件</a></li>
<li><a href="#custom-channels">自定义渠道</a></li>
</ul>
<h2 id="介绍" tabindex="-1"><a class="header-anchor" href="#介绍"><span>介绍</span></a></h2>
<p>除了支持 <a href="https://learnku.com/docs/laravel/11.x/mailmd/16682" target="_blank" rel="noopener noreferrer">发送电子邮件</a> 之外，Laravel 还支持通过各种渠道发送通知的功能，包括电子邮件、SMS（通过 <a href="https://www.vonage.com/communications-apis/" target="_blank" rel="noopener noreferrer">Vonage</a> ，以前称为 Nexmo ）和 <a href="https://slack.com/" target="_blank" rel="noopener noreferrer">Slack</a> 。此外，已经创建了多种 <a href="https://laravel-notification-channels.com/about/#suggesting-a-new-channel" target="_blank" rel="noopener noreferrer">社区构建的通知渠道</a> ，可以通过数十个不同的渠道发送通知！通知也可能存储在数据库中，以便它们可以显示在你的 Web 界面中。</p>
<p>通常，通知应该是简短的信息性消息，用于通知用户应用程序中发生的事情。例如，如果你正在编写一个账单应用，你可以通过电子邮件和 SMS 渠道向用户发送「支付凭证」通知。</p>
<h2 id="生成通知" tabindex="-1"><a class="header-anchor" href="#生成通知"><span>生成通知</span></a></h2>
<p>在 Laravel 中，每个通知都由一个类表示，该类通常存储在 <code v-pre>app/Notifications</code> 目录中。如果你在应用程序中没有看到此目录，请不要担心 - 当你运行 <code v-pre>make:notification</code> Artisan 命令时，它将为你创建：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan make:notification InvoicePaid</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>此命令将在你的 <code v-pre>app/Notifications</code> 目录中放置一个新的通知类。每个通知类都包含一个 <code v-pre>via</code> 方法和多个消息构建方法，例如 <code v-pre>toMail</code> 或 <code v-pre>toDatabase</code>，它们会针对特定的渠道把通知转换为对应的消息。</p>
<h2 id="发送通知" tabindex="-1"><a class="header-anchor" href="#发送通知"><span>发送通知</span></a></h2>
<h3 id="使用-notifiable-trait" tabindex="-1"><a class="header-anchor" href="#使用-notifiable-trait"><span>使用 Notifiable Trait</span></a></h3>
<p>通知可以通过两种方式发送：使用 <code v-pre>Notificate</code> 特性的 <code v-pre>notify</code> 方法或使用 <code v-pre>Notification</code> <a href="https://learnku.com/docs/laravel/11.x/facades" target="_blank" rel="noopener noreferrer">门面</a> 。默认情况下， <code v-pre>Notifiable</code> 特性包含在应用程序的 <code v-pre>App\Models\User</code> 模型中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Models</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Foundation<span class="token punctuation">\</span>Auth<span class="token punctuation">\</span>User</span> <span class="token keyword">as</span> Authenticatable<span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Notifiable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">User</span> <span class="token keyword">extends</span> <span class="token class-name">Authenticatable</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Notifiable</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>此 <code v-pre>notify</code> 方法需要接收一个通知实例参数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>InvoicePaid</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$user</span><span class="token operator">-></span><span class="token function">notify</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">InvoicePaid</span><span class="token punctuation">(</span><span class="token variable">$invoice</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p><strong>注意</strong><br>
请记住，你可以在任何模型上使用 <code v-pre>Notifiable</code> 特性。而不仅仅是在 <code v-pre>User</code> 模型中。</p>
</blockquote>
<h3 id="使用通知门面" tabindex="-1"><a class="header-anchor" href="#使用通知门面"><span>使用通知门面</span></a></h3>
<p>或者， 您可以通过 <code v-pre>Notification</code> <a href="https://learnku.com/docs/laravel/11.x/facades" target="_blank" rel="noopener noreferrer">门面</a>发送通知。 当您需要向多个可通知实体（例如一组用户）发送通知时，此方法非常有用。要使用 Facade 发送通知，请将所有可通知实体和通知实例传递给方法 <code v-pre>send</code> :</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Notification</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Notification</span><span class="token operator">::</span><span class="token function">send</span><span class="token punctuation">(</span><span class="token variable">$users</span><span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token class-name">InvoicePaid</span><span class="token punctuation">(</span><span class="token variable">$invoice</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>您还可以使用该 <code v-pre>sendNow</code> 方法立即发送通知。 即使通知实现了以下 <code v-pre>ShouldQueue</code> 接口，此方法也会立即发送通知:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Notification</span><span class="token operator">::</span><span class="token function">sendNow</span><span class="token punctuation">(</span><span class="token variable">$developers</span><span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token class-name">DeploymentCompleted</span><span class="token punctuation">(</span><span class="token variable">$deployment</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="制定通知渠道" tabindex="-1"><a class="header-anchor" href="#制定通知渠道"><span>制定通知渠道</span></a></h3>
<p>每个通知类都有一个 <code v-pre>via</code> 方法来确定通知将在哪些渠道上传递。通知可以在 <code v-pre>mail</code>， <code v-pre>database</code>， <code v-pre>broadcast</code>, <code v-pre>vonage</code>， 和 <code v-pre>slack</code> 渠道上发送、</p>
<blockquote>
<p>[!注意]<br>
如果您想使用其他通知渠道（如Telegram或Pusher），请查看社区驱动的 <a href="http://laravel-notification-channels.com/" target="_blank" rel="noopener noreferrer">Laravel 通知渠道网站</a>.</p>
</blockquote>
<p>该 <code v-pre>via</code> 方法接收一个 <code v-pre>$notifiable</code> 实例， 该实例将是要向其发送通知实例的类的实例。您可以使用它 <code v-pre>$notifiable</code> 来确定应在哪些渠道上传递通知:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Get the notification's delivery channels.</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">array</span></span>&lt;int, string></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">via</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">array</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$notifiable</span><span class="token operator">-></span><span class="token property">prefers_sms</span> <span class="token operator">?</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'vonage'</span><span class="token punctuation">]</span> <span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'mail'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'database'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="队列通知" tabindex="-1"><a class="header-anchor" href="#队列通知"><span>队列通知</span></a></h3>
<blockquote>
<p>[!注意]<br>
在排队通知之前，您应该配置队列并 <a href="https://learnku.com/docs/laravel/11.x/queues#running-the-queue-worker" target="_blank" rel="noopener noreferrer">启动一个工作进程</a>.</p>
</blockquote>
<p>发送通知可能需要一些时间，尤其时当渠道需要进行外部API调用来传递通知时。为了加快应用程序的响应时间，请将接口 <code v-pre>ShouldQueue</code> 和 <code v-pre>Queueable</code>特征增加到类中。以使通知排队。使用该命令生成的所有通知都已导入接口和特征 <code v-pre>make:notification</code>， 因此您可以立即将它们增加到通知类中:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Notifications</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Bus<span class="token punctuation">\</span>Queueable</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Contracts<span class="token punctuation">\</span>Queue<span class="token punctuation">\</span>ShouldQueue</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Notification</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">InvoicePaid</span> <span class="token keyword">extends</span> <span class="token class-name">Notification</span> <span class="token keyword">implements</span> <span class="token class-name">ShouldQueue</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Queueable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>一旦 <code v-pre>ShouldQueue</code> 将接口增加到通知中，您就可以像平时一样发送通知。Laravel 将检测 <code v-pre>ShouldQueue</code> 类上的接口并自动排队通知的传递:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$user</span><span class="token operator">-></span><span class="token function">notify</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">InvoicePaid</span><span class="token punctuation">(</span><span class="token variable">$invoice</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>将通知排队时，将为每个收件人和渠道组合创建一个排队作业。例如，如果您的通知有三个收件人和两个渠道，则会将六个作业分派到队列中。</p>
<h4 id="延迟通知" tabindex="-1"><a class="header-anchor" href="#延迟通知"><span>延迟通知</span></a></h4>
<p>如果您想延迟发送通知，您可以将该 <code v-pre>delay</code> 方法连接到通知实例上:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$delay</span> <span class="token operator">=</span> <span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">addMinutes</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$user</span><span class="token operator">-></span><span class="token function">notify</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">InvoicePaid</span><span class="token punctuation">(</span><span class="token variable">$invoice</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">delay</span><span class="token punctuation">(</span><span class="token variable">$delay</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>您可以将数组传递给 <code v-pre>delay</code> 方法来指定特定通道的延迟量:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$user</span><span class="token operator">-></span><span class="token function">notify</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">InvoicePaid</span><span class="token punctuation">(</span><span class="token variable">$invoice</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">delay</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'mail'</span> <span class="token operator">=></span> <span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">addMinutes</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'sms'</span> <span class="token operator">=></span> <span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">addMinutes</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>或者，您可以使用 <code v-pre>withDelay</code> 在通知类本身上定义一个方法。该 <code v-pre>withDelay</code> 方法应返回一个通道名称和延迟值的数组:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Determine the notification's delivery delay.</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">array</span></span>&lt;string, \Illuminate\Support\Carbon></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">withDelay</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">array</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'mail'</span> <span class="token operator">=></span> <span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">addMinutes</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'sms'</span> <span class="token operator">=></span> <span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">addMinutes</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="自定义通知队列连接" tabindex="-1"><a class="header-anchor" href="#自定义通知队列连接"><span>自定义通知队列连接</span></a></h4>
<p>默认情况下，排队通知将使用应用程序的默认队列连接进行排队。如果您想为特定通知指定不同的连接，您可以使用 <code v-pre>onConnection</code> 从通知的构造函数中调用该方法:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Notifications</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Bus<span class="token punctuation">\</span>Queueable</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Contracts<span class="token punctuation">\</span>Queue<span class="token punctuation">\</span>ShouldQueue</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Notification</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">InvoicePaid</span> <span class="token keyword">extends</span> <span class="token class-name">Notification</span> <span class="token keyword">implements</span> <span class="token class-name">ShouldQueue</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Queueable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * Create a new notification instance.</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">__construct</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">onConnection</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'redis'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>或者，如果您想要指定通知支持的每个通知渠道应使用的特定队列连接，您可以使用 <code v-pre>viaConnections</code> 在通知上定义一个方法。 此方法应返回一个渠道/队列连接数组:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Determine which connections should be used for each notification channel.</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">array</span></span>&lt;string, string></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">viaConnections</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">array</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'mail'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'redis'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'database'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'sync'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="自定义通知渠道队列" tabindex="-1"><a class="header-anchor" href="#自定义通知渠道队列"><span>自定义通知渠道队列</span></a></h4>
<p>如果您想要指定通知支持的每个通知渠道应使用特定队列，您可以使用 <code v-pre>viaQueues</code> 在通知上定义一个方法。此方法应返回一个渠道名称/队列名称数组:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Determine which queues should be used for each notification channel.</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">array</span></span>&lt;string, string></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">viaQueues</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">array</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'mail'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'mail-queue'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'slack'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'slack-queue'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="队列通知中间件" tabindex="-1"><a class="header-anchor" href="#队列通知中间件"><span>队列通知中间件</span></a></h4>
<p>队列通知可以像 <a href="https://learnku.com/docs/laravel/11.x/queues#job-middleware" target="_blank" rel="noopener noreferrer">队列作业</a> 一样定义中间件。 首先， <code v-pre>middleware</code> 在通知类上定义一个方法。该 <code v-pre>middleware</code> 方法将接收 <code v-pre>$notifiable</code> 和 <code v-pre>$channel</code> 变量， 这些变量允许您根据通知的目的地自定义返回的中间件:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Queue<span class="token punctuation">\</span>Middleware<span class="token punctuation">\</span>RateLimited</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Get the middleware the notification job should pass through.</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">array</span></span>&lt;int, object></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">middleware</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">,</span> <span class="token keyword type-hint">string</span> <span class="token variable">$channel</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token keyword">match</span> <span class="token punctuation">(</span><span class="token variable">$channel</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token keyword">new</span> <span class="token class-name">RateLimited</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'postmark'</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'slack'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token keyword">new</span> <span class="token class-name">RateLimited</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'slack'</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token keyword">default</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="队列通知和数据库事务" tabindex="-1"><a class="header-anchor" href="#队列通知和数据库事务"><span>队列通知和数据库事务</span></a></h4>
<p>当队列通知在数据库事务中分派时，它们可能会在数据库事务提交前由队列处理。当发生这种情况时，您在数据库事务期间对模型或者数据库记录所做的任何更新可能尚未反映在数据库中。此外，在事务中创建的任何数据库记录可能都不存在于数据库中。如果您的通知依赖这些模型，则在处理发送队列通知的作业时可能会发生意外错误。</p>
<p>如果您的队列连接的 <code v-pre>after_commit</code> 配置选项设置为 <code v-pre>false</code>，您任然可以通知在发送通知时调用该 <code v-pre>afterCommit</code> 方法来提交所有数据库事务之后应分派的队列通知:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>InvoicePaid</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$user</span><span class="token operator">-></span><span class="token function">notify</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">InvoicePaid</span><span class="token punctuation">(</span><span class="token variable">$invoice</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">afterCommit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>或者，您可以使用 <code v-pre>afterCommit</code> 从通知的构造函数中调用该方法:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Notifications</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Bus<span class="token punctuation">\</span>Queueable</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Contracts<span class="token punctuation">\</span>Queue<span class="token punctuation">\</span>ShouldQueue</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Notification</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">InvoicePaid</span> <span class="token keyword">extends</span> <span class="token class-name">Notification</span> <span class="token keyword">implements</span> <span class="token class-name">ShouldQueue</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Queueable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * Create a new notification instance.</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">__construct</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">afterCommit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>[!注意]<br>
要了解有关解决这些问题的更多信息，请参阅 <a href="https://learnku.com/docs/laravel/11.x/queues#jobs-and-database-transactions" target="_blank" rel="noopener noreferrer">消息队列和数据事务</a>的文档。</p>
</blockquote>
<h4 id="确定是否应发送队列通知" tabindex="-1"><a class="header-anchor" href="#确定是否应发送队列通知"><span>确定是否应发送队列通知</span></a></h4>
<p>在将队列通知发送到队列后惊醒后台处理之后，它通常会被队列工作者接收并发送给其预期的收件人。</p>
<p>但是，如果您想在队列工作者处理完队列通知后，最终决定是否发送该通知，您可以使用 <code v-pre>shouldSend</code> 在通知类上定义个方法，如果此方法返回 <code v-pre>false</code>，则不会发送通知:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Determine if the notification should be sent.</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">shouldSend</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">,</span> <span class="token keyword type-hint">string</span> <span class="token variable">$channel</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">bool</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">invoice</span><span class="token operator">-></span><span class="token function">isPaid</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="按需通知" tabindex="-1"><a class="header-anchor" href="#按需通知"><span>按需通知</span></a></h3>
<p>有时您可能需要向未储存为应该程序“用户”的人发送通知。使用 <code v-pre>Notification</code> facade 的 <code v-pre>route</code> 方法， 您可以在发送通知之前指定临时通知的路由信息:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Broadcasting<span class="token punctuation">\</span>Channel</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Notification</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Notification</span><span class="token operator">::</span><span class="token function">route</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'mail'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'taylor@example.com'</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">route</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'vonage'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'5555555555'</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">route</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'slack'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'#slack-channel'</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">route</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'broadcast'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token keyword">new</span> <span class="token class-name">Channel</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'channel-name'</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">notify</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">InvoicePaid</span><span class="token punctuation">(</span><span class="token variable">$invoice</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果您想在向路线发送按需通知时提供收件人的 <code v-pre>mail</code> 名称，您可以提供一个数组，其中包含电子邮件地址作为键，并以姓名作为数组中的第一个元素的值:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Notification</span><span class="token operator">::</span><span class="token function">route</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'mail'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'barrett@example.com'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Barrett Blair'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">notify</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">InvoicePaid</span><span class="token punctuation">(</span><span class="token variable">$invoice</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>使用 <code v-pre>routes</code> 方法，您可以同时为多个通知渠道提供临时路由信息:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Notification</span><span class="token operator">::</span><span class="token function">routes</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'mail'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'barrett@example.com'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Barrett Blair'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'vonage'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'5555555555'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">notify</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">InvoicePaid</span><span class="token punctuation">(</span><span class="token variable">$invoice</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="邮件通知" tabindex="-1"><a class="header-anchor" href="#邮件通知"><span>邮件通知</span></a></h2>
<h3 id="格式化邮件消息" tabindex="-1"><a class="header-anchor" href="#格式化邮件消息"><span>格式化邮件消息</span></a></h3>
<p>如果通知支持以电子邮件形式发送，则应使用 <code v-pre>toMail</code> 在通知类上定义一个方法。 此方法接受一个 <code v-pre>$notifiable</code> 实体并返回 <code v-pre>Illuminate\Notifications\Messages\MailMessage</code> 实例。</p>
<p>此类 <code v-pre>MailMessage</code> 包含一些简单的方法，可帮助您构建事务性电子邮件消息。邮件消息可能包含多行文本以及 &quot;点击按钮&quot; 。让我们看一个 <code v-pre>toMail</code> 方法示例:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Get the mail representation of the notification.</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toMail</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">MailMessage</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$url</span> <span class="token operator">=</span> <span class="token function">url</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/invoice/'</span><span class="token operator">.</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token property">invoice</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">MailMessage</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">greeting</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Hello!'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">line</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'One of your invoices has been paid!'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">lineIf</span><span class="token punctuation">(</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token property">amount</span> <span class="token operator">></span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token string double-quoted-string">"Amount paid: <span class="token interpolation"><span class="token punctuation">{</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token property">amount</span><span class="token punctuation">}</span></span>"</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">action</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'View Invoice'</span><span class="token punctuation">,</span> <span class="token variable">$url</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">line</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Thank you for using our application!'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>[!注意]<br>
我们在 <code v-pre>toMail</code> 方法中使用 <code v-pre>$this-&gt;invoice-&gt;id</code>。 您可以将通知生成消息所需的任何数据传递到通知的构造函数中。</p>
</blockquote>
<p>在此示例中，我们注册了问候语，一行文本、点击按钮，然后是宁一行文本。此对象 <code v-pre>MailMessage</code> 提供这些方法使格式化小型交易电子邮件变得简单而快速。然后，邮件渠道将消息组件转换为带有纯文本副本的美观、响应式HTML电子邮件模版。 以下是 <code v-pre>mail</code> 生成的电子邮件示例:</p>
<p><img src="https://laravel.com/img/docs/notification-example-2.png" alt="notification-example-2.png"></p>
<blockquote>
<p>[!注意]<br>
当发送邮件通知时，请务必在配置文件 <code v-pre>config/app.php</code> 中配置 <code v-pre>name</code> 选项。此值将用于邮件通知消息的页眉和页脚。</p>
</blockquote>
<h4 id="错误消息" tabindex="-1"><a class="header-anchor" href="#错误消息"><span>错误消息</span></a></h4>
<p>某些通知恢告知用户错误，例如发票付款失败。您可以使用 <code v-pre>error</code> 在构建邮件时调用该方法来表明邮件消息与错误有关。在邮件消息使用该 <code v-pre>error</code> 方法时，点击按钮将为红色而不是黑色:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Get the mail representation of the notification.</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toMail</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">MailMessage</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">MailMessage</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">error</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">subject</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Invoice Payment Failed'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">line</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'...'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="其他邮件通知格式选项" tabindex="-1"><a class="header-anchor" href="#其他邮件通知格式选项"><span>其他邮件通知格式选项</span></a></h4>
<p>您无需在通知类中定义文本行，而是可以使用 <code v-pre>view</code> 方法指定用户呈现电子邮件通知的自定义模版:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Get the mail representation of the notification.</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toMail</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">MailMessage</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">MailMessage</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">view</span><span class="token punctuation">(</span></span>
<span class="line">        <span class="token string single-quoted-string">'mail.invoice.paid'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'invoice'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">invoice</span><span class="token punctuation">]</span></span>
<span class="line">    <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>您可以通过 <code v-pre>view</code> 将视图名称作为提供给该方法的数组的第二个元素来制定邮件消息的纯文本视图:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Get the mail representation of the notification.</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toMail</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">MailMessage</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">MailMessage</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">view</span><span class="token punctuation">(</span></span>
<span class="line">        <span class="token punctuation">[</span><span class="token string single-quoted-string">'mail.invoice.paid'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'mail.invoice.paid-text'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">[</span><span class="token string single-quoted-string">'invoice'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">invoice</span><span class="token punctuation">]</span></span>
<span class="line">    <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>或者，如果您的消息只有纯文本视图，你可以使用 <code v-pre>text</code> 方法:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Get the mail representation of the notification.</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toMail</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">MailMessage</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">MailMessage</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">text</span><span class="token punctuation">(</span></span>
<span class="line">        <span class="token string single-quoted-string">'mail.invoice.paid-text'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'invoice'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">invoice</span><span class="token punctuation">]</span></span>
<span class="line">    <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="自定义发件人" tabindex="-1"><a class="header-anchor" href="#自定义发件人"><span>自定义发件人</span></a></h3>
<p>默认情况下，电子邮件的发送人/发件人地址在配置文件 <code v-pre>config/mail.php</code> 中定义。但是，您可以使用 <code v-pre>from</code> 方法为特定通知指定发件人地址:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Get the mail representation of the notification.</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toMail</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">MailMessage</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">MailMessage</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">from</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'barrett@example.com'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Barrett Blair'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">line</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'...'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="自定义收件人" tabindex="-1"><a class="header-anchor" href="#自定义收件人"><span>自定义收件人</span></a></h3>
<p>通过 <code v-pre>mail</code> 渠道发送通知时，通知系统将自动查找可通知实体上的属性。您可以通过在 <code v-pre>email</code> 中定义 <code v-pre>routeNotificationForMail</code> 方法来自定义用户发送通知的电子邮件地址:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Models</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Foundation<span class="token punctuation">\</span>Auth<span class="token punctuation">\</span>User</span> <span class="token keyword">as</span> Authenticatable<span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Notifiable</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Notification</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">User</span> <span class="token keyword">extends</span> <span class="token class-name">Authenticatable</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Notifiable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * Route notifications for the mail channel.</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@return</span>  <span class="token class-name"><span class="token keyword">array</span></span>&lt;string, string>|string</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">routeNotificationForMail</span><span class="token punctuation">(</span><span class="token class-name type-declaration">Notification</span> <span class="token variable">$notification</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">array</span><span class="token operator">|</span><span class="token keyword type-declaration">string</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token comment">// Return email address only...</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">email_address</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// Return email address and name...</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token punctuation">[</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token property">email_address</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">name</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="自定义主题" tabindex="-1"><a class="header-anchor" href="#自定义主题"><span>自定义主题</span></a></h3>
<p>默认情况下，电子邮件的主题是通知的类名，格式为标题大小写。因此，如果您的通知类名为 <code v-pre>InvoicePaid</code>，则电子邮件的主题将是 <code v-pre>Invoice Paid</code>。如果您想为消息指定不同的主题，你可以使用 <code v-pre>subject</code> 在构建消息时调用该方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Get the mail representation of the notification.</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toMail</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">MailMessage</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">MailMessage</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">subject</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Notification Subject'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">line</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'...'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="自定义邮件程序" tabindex="-1"><a class="header-anchor" href="#自定义邮件程序"><span>自定义邮件程序</span></a></h3>
<p>默认情况下，电子邮件通知将使用 <code v-pre>config/mail.php</code> 中定义的默认邮件程序发送。但是，您可以在构建时调用 <code v-pre>mailer</code> 方法指定其他邮件程序：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Get the mail representation of the notification.</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toMail</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">MailMessage</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">MailMessage</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">mailer</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'postmark'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">line</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'...'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="自定义模版" tabindex="-1"><a class="header-anchor" href="#自定义模版"><span>自定义模版</span></a></h3>
<p>您可以通过发布通知包的资源来修改邮件通知使用的HTML和纯文本模版。运行此命令后。邮件通知模版将位于以下 <code v-pre>resources/views/vendor/notifications</code> 目录中：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan vendor:publish <span class="token parameter variable">--tag</span><span class="token operator">=</span>laravel-notifications</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="附件" tabindex="-1"><a class="header-anchor" href="#附件"><span>附件</span></a></h3>
<p>要将附件增加到电子邮件中，请在构建时使用 <code v-pre>attach</code> 方法。 该 <code v-pre>attach</code> 方法接收文件的绝对路径作为其第一个参数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Get the mail representation of the notification.</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toMail</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">MailMessage</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">MailMessage</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">greeting</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Hello!'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">attach</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/path/to/file'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>[!注意]<br>
通知邮件提供的 <code v-pre>attach</code> 方法也接受可 <a href="https://learnku.com/docs/laravel/11.x/mail#attachable-objects" target="_blank" rel="noopener noreferrer">附加对象</a>。 请参阅更多的 <a href="https://learnku.com/docs/laravel/11.x/mail#attachable-objects" target="_blank" rel="noopener noreferrer">可附加对象文档</a> 以了解更多信息。</p>
</blockquote>
<p>将文件附加到消息时，您还可以通过将 <code v-pre>array</code> 第二个参数传递给 <code v-pre>attach</code> 方法来指定显示名称或MIME类型：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Get the mail representation of the notification.</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toMail</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">MailMessage</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">MailMessage</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">greeting</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Hello!'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">attach</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/path/to/file'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">                    <span class="token string single-quoted-string">'as'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'name.pdf'</span><span class="token punctuation">,</span></span>
<span class="line">                    <span class="token string single-quoted-string">'mime'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'application/pdf'</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>与在可邮件对象中附件文件不同，您不能使用 <code v-pre>attachFromStorage</code> 直接从存储磁盘附加文件。您应该使用 <code v-pre>attach</code>方法来指定存储磁盘上文件的绝对路径。或者，您可以从 <code v-pre>toMail</code> 该方法返回 <a href="https://learnku.com/docs/laravel/11.x/mail#generating-mailables" target="_blank" rel="noopener noreferrer">可邮件文件</a> ：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Mail<span class="token punctuation">\</span>InvoicePaid</span> <span class="token keyword">as</span> InvoicePaidMailable<span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Get the mail representation of the notification.</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toMail</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">Mailable</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">InvoicePaidMailable</span><span class="token punctuation">(</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token property">invoice</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">to</span><span class="token punctuation">(</span><span class="token variable">$notifiable</span><span class="token operator">-></span><span class="token property">email</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">attachFromStorage</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/path/to/file'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>必要时，可是使用 <code v-pre>attachMany</code> 方法将多个文件附加到一条消息中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Get the mail representation of the notification.</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toMail</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">MailMessage</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">MailMessage</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">greeting</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Hello!'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">attachMany</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">                    <span class="token string single-quoted-string">'/path/to/forge.svg'</span><span class="token punctuation">,</span></span>
<span class="line">                    <span class="token string single-quoted-string">'/path/to/vapor.svg'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">                        <span class="token string single-quoted-string">'as'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Logo.svg'</span><span class="token punctuation">,</span></span>
<span class="line">                        <span class="token string single-quoted-string">'mime'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'image/svg+xml'</span><span class="token punctuation">,</span></span>
<span class="line">                    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="原始数据附件" tabindex="-1"><a class="header-anchor" href="#原始数据附件"><span>原始数据附件</span></a></h4>
<p>该 <code v-pre>attachData</code> 方法可用户将原始字节字符串作为附件。调用 <code v-pre>attachData</code> 方法时，应提供分配给附件的文件名：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Get the mail representation of the notification.</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toMail</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">MailMessage</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">MailMessage</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">greeting</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Hello!'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">attachData</span><span class="token punctuation">(</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token property">pdf</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name.pdf'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">                    <span class="token string single-quoted-string">'mime'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'application/pdf'</span><span class="token punctuation">,</span></span>
<span class="line">                <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="增加标签和元数据" tabindex="-1"><a class="header-anchor" href="#增加标签和元数据"><span>增加标签和元数据</span></a></h3>
<p>一些第三方电子邮件提供商（例如 Mailgun 和 Postmark）支持消息标签和元数据，可用户对应应用程序发送的电子邮件进行分组和跟踪。您可以通过 <code v-pre>tag</code> 和 <code v-pre>metadata</code> 方法向电子邮件消息增加标签和元数据：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Get the mail representation of the notification.</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toMail</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">MailMessage</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">MailMessage</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">greeting</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Comment Upvoted!'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">tag</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'upvote'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">metadata</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'comment_id'</span><span class="token punctuation">,</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">comment</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果您的运用程序正在使用 Mailgun 驱动程序，您可以查阅 Mialgun 的文档以获取有关 <a href="https://documentation.mailgun.com/en/latest/user_manual.html#tagging-1" target="_blank" rel="noopener noreferrer">标签</a> 和 <a href="https://documentation.mailgun.com/en/latest/user_manual.html#attaching-data-to-messages" target="_blank" rel="noopener noreferrer">元数据</a> 的更多信息。 同样，您可以查阅 Postmark 文档以获取有关 <a href="https://postmarkapp.com/blog/tags-support-for-smtp" target="_blank" rel="noopener noreferrer">标签</a> 和 <a href="https://postmarkapp.com/support/article/1125-custom-metadata-faq" target="_blank" rel="noopener noreferrer">元数据</a> 的更多信息。</p>
<p>如果您的应用程序使用 Amazon SES 发送电子邮件，您应该使用 <code v-pre>metadata</code> 方法将 <a href="https://docs.aws.amazon.com/ses/latest/APIReference/API_MessageTag.html" target="_blank" rel="noopener noreferrer">SES &quot;标签&quot;</a> 附加到消息。</p>
<h3 id="自定义-symfony-消息" tabindex="-1"><a class="header-anchor" href="#自定义-symfony-消息"><span>自定义 Symfony 消息</span></a></h3>
<p>该 <code v-pre>MailMessage</code> 类的 <code v-pre>withSymfonyMessage</code> 方法允许您注册一个闭包，该闭包将在消息发送之前使用 Symfony Message 实例调用。这使您有机会在消息发送之前对其进行深度自定义：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Symfony<span class="token punctuation">\</span>Component<span class="token punctuation">\</span>Mime<span class="token punctuation">\</span>Email</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Get the mail representation of the notification.</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toMail</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">MailMessage</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">MailMessage</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">withSymfonyMessage</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Email</span> <span class="token variable">$message</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">                    <span class="token variable">$message</span><span class="token operator">-></span><span class="token function">getHeaders</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">addTextHeader</span><span class="token punctuation">(</span></span>
<span class="line">                        <span class="token string single-quoted-string">'Custom-Header'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Header Value'</span></span>
<span class="line">                    <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">                <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="使用邮件资料" tabindex="-1"><a class="header-anchor" href="#使用邮件资料"><span>使用邮件资料</span></a></h3>
<p>如果需要，您可以从通知的 <code v-pre>toMail</code> 方法中返回完整的 <a href="https://learnku.com/docs/laravel/11.x/mail" target="_blank" rel="noopener noreferrer">邮件资料</a>。当返回 <code v-pre>Mailable</code> 而不是 <code v-pre>MailMessage</code> 时，您需要使用可邮件对象的 <code v-pre>to</code> 方法指定消息收件人：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Mail<span class="token punctuation">\</span>InvoicePaid</span> <span class="token keyword">as</span> InvoicePaidMailable<span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Mail<span class="token punctuation">\</span>Mailable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Get the mail representation of the notification.</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toMail</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">Mailable</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">InvoicePaidMailable</span><span class="token punctuation">(</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token property">invoice</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">to</span><span class="token punctuation">(</span><span class="token variable">$notifiable</span><span class="token operator">-></span><span class="token property">email</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="邮件资料和按需通知" tabindex="-1"><a class="header-anchor" href="#邮件资料和按需通知"><span>邮件资料和按需通知</span></a></h4>
<p>如果您要发送 <a href="#on-demand-notifications">按需通知</a>，则 <code v-pre>Illuminate\Notifications\AnonymousNotifiable</code> 实例赋予 <code v-pre>toMail</code> 方法 <code v-pre>$notifiable</code> ，它提供一种可用于检索应将按需通知发送到电子邮件的 <code v-pre>routeNotificationFor</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Mail<span class="token punctuation">\</span>InvoicePaid</span> <span class="token keyword">as</span> InvoicePaidMailable<span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>AnonymousNotifiable</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Mail<span class="token punctuation">\</span>Mailable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Get the mail representation of the notification.</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toMail</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">Mailable</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$address</span> <span class="token operator">=</span> <span class="token variable">$notifiable</span> <span class="token keyword">instanceof</span> <span class="token class-name">AnonymousNotifiable</span></span>
<span class="line">            <span class="token operator">?</span> <span class="token variable">$notifiable</span><span class="token operator">-></span><span class="token function">routeNotificationFor</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'mail'</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token punctuation">:</span> <span class="token variable">$notifiable</span><span class="token operator">-></span><span class="token property">email</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">InvoicePaidMailable</span><span class="token punctuation">(</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token property">invoice</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">to</span><span class="token punctuation">(</span><span class="token variable">$address</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="预览邮件通知" tabindex="-1"><a class="header-anchor" href="#预览邮件通知"><span>预览邮件通知</span></a></h3>
<p>在设计邮件通知模版时，可以像典型的 Blade 模版一样在浏览器中快速预览并呈现的邮件消息，这很方便。因此 Laravel 允许您直接从路由闭包或者控制器返回邮件通知生成的任何邮件消息。 <code v-pre>MailMessage</code> 返回后，它将在浏览器中呈现并显示，让您可以快速预览其设计，而无需将其发送到实际的电子邮件地址：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>Invoice</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>InvoicePaid</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/notification'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$invoice</span> <span class="token operator">=</span> <span class="token class-name static-context">Invoice</span><span class="token operator">::</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">InvoicePaid</span><span class="token punctuation">(</span><span class="token variable">$invoice</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">toMail</span><span class="token punctuation">(</span><span class="token variable">$invoice</span><span class="token operator">-></span><span class="token property">user</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="markdown-邮件通知" tabindex="-1"><a class="header-anchor" href="#markdown-邮件通知"><span>Markdown 邮件通知</span></a></h2>
<p>Markdown 邮件通知让您能够利用预先构建的邮件通知模版，同时让您更自由地编写更长的自定义消息。由于消息是用 Markdown 编写的，因此 Laravel 能够为消息呈现美观、响应迅速的 HTML 模版，同时还会自动生成纯文本副本。</p>
<h3 id="生成消息" tabindex="-1"><a class="header-anchor" href="#生成消息"><span>生成消息</span></a></h3>
<p>要使用响应的 Markdown 模版生成通知，您可以使用 Artisan 命令 <code v-pre>make:notification</code> 的选项 <code v-pre>--markdown</code>：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan make:notification InvoicePaid <span class="token parameter variable">--markdown</span><span class="token operator">=</span>mail.invoice.paid</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>与所有其他邮件通知一样，使用 Markdown 模版的通知应在通知类定义一个 <code v-pre>toMail</code> 方法。但是，不要使用 <code v-pre>line</code> 和 <code v-pre>action</code> 方法来构造通知，而是使用 <code v-pre>markdown</code> 方法来指定使用的 Markdown 模版名称。您希望提供给模版的数据数组可以作为方法的第二个参数传递：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Get the mail representation of the notification.</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toMail</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">MailMessage</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$url</span> <span class="token operator">=</span> <span class="token function">url</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/invoice/'</span><span class="token operator">.</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token property">invoice</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">MailMessage</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">subject</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Invoice Paid'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">markdown</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'mail.invoice.paid'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'url'</span> <span class="token operator">=></span> <span class="token variable">$url</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="撰写消息" tabindex="-1"><a class="header-anchor" href="#撰写消息"><span>撰写消息</span></a></h3>
<p>Markdown 邮件通知使用 Blade 组件和 Markdown 语法的组合。让您可以轻松构建通知，同时利用 Laravel 预先制作的通知组件：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">&lt;x-mail::message&gt;</span>
<span class="line"># Invoice Paid</span>
<span class="line"></span>
<span class="line">Your invoice has been paid!</span>
<span class="line"></span>
<span class="line">&lt;x-mail::button :url=&quot;$url&quot;&gt;</span>
<span class="line">View Invoice</span>
<span class="line">&lt;/x-mail::button&gt;</span>
<span class="line"></span>
<span class="line">Thanks,&lt;br&gt;</span>
<span class="line">{{ config('app.name') }}</span>
<span class="line">&lt;/x-mail::message&gt;</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="按钮组件" tabindex="-1"><a class="header-anchor" href="#按钮组件"><span>按钮组件</span></a></h4>
<p>按钮组件呈现居中的按钮链接。该组件接收一个 <code v-pre>url</code> 和一个 <code v-pre>color</code> 参数。支持的颜色有 <code v-pre>primary</code>， <code v-pre>green</code>， 和 <code v-pre>red</code>。您可以根据需要向通知增加任意数量的按钮组件：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">&lt;x-mail::button :url=&quot;$url&quot; color=&quot;green&quot;&gt;</span>
<span class="line">View Invoice</span>
<span class="line">&lt;/x-mail::button&gt;</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="面板组件" tabindex="-1"><a class="header-anchor" href="#面板组件"><span>面板组件</span></a></h4>
<p>面板组件将给定文本块呈现在面板中，该面板的背景颜色与通知的其余部分略有不同。这可以让您将注意力吸引到给定的文本块上：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">&lt;x-mail::panel&gt;</span>
<span class="line">This is the panel content.</span>
<span class="line">&lt;/x-mail::panel&gt;</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="表格组件" tabindex="-1"><a class="header-anchor" href="#表格组件"><span>表格组件</span></a></h4>
<p>表格组件允许您将 Markdown 表格转换为 HTML 表格。该组件接受 Markdown 表格作为其内容。使默认的 Markdown 表格对齐语法支持表格列对齐：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">&lt;x-mail::table&gt;</span>
<span class="line">| Laravel       | Table         | Example  |</span>
<span class="line">| ------------- |:-------------:| --------:|</span>
<span class="line">| Col 2 is      | Centered      | $10      |</span>
<span class="line">| Col 3 is      | Right-Aligned | $20      |</span>
<span class="line">&lt;/x-mail::table&gt;</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="自定义组件" tabindex="-1"><a class="header-anchor" href="#自定义组件"><span>自定义组件</span></a></h3>
<p>您可以将所有 Markdown 通知组件导出到您自己的运用程序进行自定义。要导出组件，请使用 Artisan 命令的 <code v-pre>vendor:publish</code> 方法发布 <code v-pre>laravel-mail</code> 资产标签：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan vendor:publish <span class="token parameter variable">--tag</span><span class="token operator">=</span>laravel-mail</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>此命令将会将 Markdown 邮件组件发布到 <code v-pre>resources/views/vendor/mail</code> 目录。 <code v-pre>mail</code> 将包含一个 <code v-pre>html</code> 和一个 <code v-pre>text</code> 目录，每个目录包含每个可用组件的各自表示。您可以任意自定义这些组件。</p>
<h4 id="自定义css" tabindex="-1"><a class="header-anchor" href="#自定义css"><span>自定义CSS</span></a></h4>
<p>导出组建后， <code v-pre>resources/views/vendor/mail/html/themes</code> 目录将包含一个 <code v-pre>default.css</code> 文件。您可以在此文件中自定义CSS，并且您的样式将自动内联到 Markdown 通知的 HTML 表示中。</p>
<p>如果您想要为 Laravel 的 Markdown 组件构建一个全新的主题，您可以在 <code v-pre>html/themes</code> 目录中放置一个CSS文件。 命名并保存 CSS 文件后，更新配置文件 <code v-pre>mail</code> 的 <code v-pre>theme</code> 选项以匹配新主题名称。</p>
<p>要自定义单个通知主题，您可以在构建通知邮件消息时调用 <code v-pre>theme</code> 方法，该 <code v-pre>theme</code> 方法接受发送通知时应使用的主题名称：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Get the mail representation of the notification.</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toMail</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">MailMessage</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">MailMessage</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">theme</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'invoice'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">subject</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Invoice Paid'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">markdown</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'mail.invoice.paid'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'url'</span> <span class="token operator">=></span> <span class="token variable">$url</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="数据库通知" tabindex="-1"><a class="header-anchor" href="#数据库通知"><span>数据库通知</span></a></h2>
<h3 id="先决条件" tabindex="-1"><a class="header-anchor" href="#先决条件"><span>先决条件</span></a></h3>
<p>通知渠道 <code v-pre>database</code> 将通知消息存储在数据库表中，该表将包含通知类型等信息以及描述通知的 JSON 数据结构。</p>
<p>您可以查询该表以在应用程序的用户界面显示通知。但是，在执行操作前，您需要创建一个数据库来保存通知。您可以使用以下命令 <code v-pre>make:notifications-table</code> 生成具有正确表结构的 <a href="https://learnku.com/docs/laravel/11.x/migrations" target="_blank" rel="noopener noreferrer">数据库迁移</a>文件：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan make:notifications-table</span>
<span class="line"></span>
<span class="line">php artisan migrate</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>[!注意]<br>
如果您的通知模型使用 <a href="https://learnku.com/docs/laravel/11.x/eloquent#uuid-and-ulid-keys" target="_blank" rel="noopener noreferrer">UUID or ULID 主键</a>，则应在通知表迁移中用 <a href="https://learnku.com/docs/laravel/11.x/migrations#column-method-uuidMorphs" target="_blank" rel="noopener noreferrer"><code v-pre>uuidMorphs</code></a> 或 <a href="https://learnku.com/docs/laravel/11.x/migrations#column-method-ulidMorphs" target="_blank" rel="noopener noreferrer"><code v-pre>ulidMorphs</code></a> 替换 <code v-pre>morphs</code> 方法。</p>
</blockquote>
<h3 id="格式化数据库通知" tabindex="-1"><a class="header-anchor" href="#格式化数据库通知"><span>格式化数据库通知</span></a></h3>
<p>如果通知支持存储在数据库表中，则应在通知类上定义 <code v-pre>toDatabase</code> 或 <code v-pre>toArray</code>方法。<code v-pre>$notifiable</code> 方法将接收一个实体并返回一个纯 PHP 数组。返回的数组 <code v-pre>data</code> 将被编码为 JSON 并存储在表 <code v-pre>notifications</code> 的列中。让我们看一个 <code v-pre>toArray</code> 示例方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Get the array representation of the notification.</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">array</span></span>&lt;string, mixed></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toArray</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">array</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'invoice_id'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">invoice</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'amount'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">invoice</span><span class="token operator">-></span><span class="token property">amount</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>当通知存储在应用程序的数据库中时， <code v-pre>type</code> 列将填充通知的类名。但是，您可以通过 <code v-pre>databaseType</code> 方法在通知类上定义方法来自定义此行为：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Get the notification's database type.</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">string</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">databaseType</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">string</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token string single-quoted-string">'invoice-paid'</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="todatabase-对比-toarray" tabindex="-1"><a class="header-anchor" href="#todatabase-对比-toarray"><span><code v-pre>toDatabase</code> 对比 <code v-pre>toArray</code></span></a></h4>
<p><code v-pre>toArray</code> 方法还被通道用来 <code v-pre>broadcast</code> 决定要向 JavaScript 驱动的前段广播哪些数据。如果您希望为 <code v-pre>database</code> 和 <code v-pre>broadcast</code> 通道提供两种不同数组来表示， 则应该定义一个 <code v-pre>toDatabase</code> 方法而不是一个 <code v-pre>toArray</code> 方法。</p>
<h3 id="访问通知" tabindex="-1"><a class="header-anchor" href="#访问通知"><span>访问通知</span></a></h3>
<p>一旦通知存储在数据库中，您需要一种方便的方法从通知实体访问它们。 <code v-pre>Illuminate\Notifications\Notifiable</code> 特征包含在 Laravel 默认的 <code v-pre>App\Models\User</code> 模型中，包括一个 <code v-pre>notifications</code> <a href="https://learnku.com/docs/laravel/11.x/eloquent-relationships" target="_blank" rel="noopener noreferrer">Eloquent 关系</a> ，该关系返回实体的通知，要获取通知，您可以向其他 Eloquent 关系一样访问此方法。默认情况下，通知将 <code v-pre>created_at</code> 时间戳排序，最新通知位于集合的开头：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$user</span> <span class="token operator">=</span> <span class="token class-name class-name-fully-qualified static-context">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>User</span><span class="token operator">::</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$user</span><span class="token operator">-></span><span class="token property">notifications</span> <span class="token keyword">as</span> <span class="token variable">$notification</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">echo</span> <span class="token variable">$notification</span><span class="token operator">-></span><span class="token property">type</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果您只想检索未读通知，则可以使用 <code v-pre>unreadNotifications</code> 关系。 同样，这些通知将按 <code v-pre>created_at</code> 时间戳排序，最新通知位于集合开头：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$user</span> <span class="token operator">=</span> <span class="token class-name class-name-fully-qualified static-context">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>User</span><span class="token operator">::</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$user</span><span class="token operator">-></span><span class="token property">unreadNotifications</span> <span class="token keyword">as</span> <span class="token variable">$notification</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">echo</span> <span class="token variable">$notification</span><span class="token operator">-></span><span class="token property">type</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>[!注意]<br>
要从 JavaScript 客户端访问通知，您应该为应用程序定义一个通知控制器，该控制器返回可通知实体（例如当前用户）的通知。然后，您可以从 JavaScript 客户端向该控制器的 URL 发出 HTTP 请求。</p>
</blockquote>
<h3 id="将通知标记为已读" tabindex="-1"><a class="header-anchor" href="#将通知标记为已读"><span>将通知标记为已读</span></a></h3>
<p>通常，当用户查看通知时，您会希望将其标记为已读。 <code v-pre>Illuminate\Notifications\Notifiable</code> 特征提供了一种 <code v-pre>markAsRead</code> 方法，可更新数据库记录上的 <code v-pre>read_at</code> 列：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$user</span> <span class="token operator">=</span> <span class="token class-name class-name-fully-qualified static-context">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>User</span><span class="token operator">::</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$user</span><span class="token operator">-></span><span class="token property">unreadNotifications</span> <span class="token keyword">as</span> <span class="token variable">$notification</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$notification</span><span class="token operator">-></span><span class="token function">markAsRead</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>但是，您可以直接对通知集合使用 <code v-pre>markAsRead</code> 方法，而不是循环遍历每个通知：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$user</span><span class="token operator">-></span><span class="token property">unreadNotifications</span><span class="token operator">-></span><span class="token function">markAsRead</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>您还可以使用批量更新查询将所有的通知标记为已读，而无需从数据库中检索它们：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$user</span> <span class="token operator">=</span> <span class="token class-name class-name-fully-qualified static-context">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>User</span><span class="token operator">::</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$user</span><span class="token operator">-></span><span class="token function">unreadNotifications</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">update</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'read_at'</span> <span class="token operator">=></span> <span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你还可以使用 <code v-pre>delete</code> 将其从表中完全删除：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$user</span><span class="token operator">-></span><span class="token function">notifications</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">delete</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="广播通知" tabindex="-1"><a class="header-anchor" href="#广播通知"><span>广播通知</span></a></h2>
<h3 id="先决条件-1" tabindex="-1"><a class="header-anchor" href="#先决条件-1"><span>先决条件</span></a></h3>
<p>在广播通知之前，您应该配置并熟悉 Laravel 的 <a href="https://learnku.com/docs/laravel/11.x/broadcasting" target="_blank" rel="noopener noreferrer">事件广播</a> 服务，事件广播提供了一种从 JavaScript 驱动的前段对服务器端 Laravel 事件做出反应的方法。</p>
<h3 id="格式化广播通知" tabindex="-1"><a class="header-anchor" href="#格式化广播通知"><span>格式化广播通知</span></a></h3>
<p><code v-pre>broadcast</code> 渠道使用 Laravel 的 <a href="https://learnku.com/docs/laravel/11.x/broadcasting" target="_blank" rel="noopener noreferrer">事件广播</a> 服务。 允许您的 JavaScript 前段实时捕获通知。如果通知支持广播，您可以在通知类上定义一个 <code v-pre>toBroadcast</code>方法。该方法将接收一个 <code v-pre>$notifiable</code> 实体返回一个 <code v-pre>BroadcastMessage</code> 实例。如果 <code v-pre>toArray</code> 方法不存在，则该方法将用于收集应广播的数据。返回的数据将被编码为 JSON 并广播到您的 JavaScript 前端。让我们看一个 <code v-pre>toBroadcast</code> 方法示例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Messages<span class="token punctuation">\</span>BroadcastMessage</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Get the broadcastable representation of the notification.</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toBroadcast</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">BroadcastMessage</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">BroadcastMessage</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'invoice_id'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">invoice</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'amount'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">invoice</span><span class="token operator">-></span><span class="token property">amount</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="广播队列配置" tabindex="-1"><a class="header-anchor" href="#广播队列配置"><span>广播队列配置</span></a></h4>
<p>所有广播通知都排队等待广播。如果您想要配置用于排队广播操作的队列连接或者队列名称，您可以使用 <code v-pre>BroadcastMessage</code> 的 <code v-pre>onConnection</code> 和 <code v-pre>onQueue</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">BroadcastMessage</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">onConnection</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'sqs'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">onQueue</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'broadcasts'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="自定义通知类型" tabindex="-1"><a class="header-anchor" href="#自定义通知类型"><span>自定义通知类型</span></a></h4>
<p>除了您指定的数据外，所有广播通知还具有一个 包含通知完整类名的 <code v-pre>type</code> 字段，如果你想自定义通知 <code v-pre>type</code> ，您可以在通知类定义一个 <code v-pre>broadcastType</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * Get the type of the notification being broadcast.</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">broadcastType</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">string</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token string single-quoted-string">'broadcast.message'</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="监听通知" tabindex="-1"><a class="header-anchor" href="#监听通知"><span>监听通知</span></a></h3>
<p>通知将在使用约定格式 <code v-pre>{notifiable}.{id}</code> 的私有频道上广播。因此，如果您向 ID 为1的 <code v-pre>App\Models\User</code> 实例发送通知，则通知将在私有频道上广播。使用 <a href="https://learnku.com/docs/laravel/11.x/broadcasting#client-side-installation" target="_blank" rel="noopener noreferrer">Laravel Echo</a>，您可以使用 <code v-pre>notification</code> 方法轻松地在频道上监听通知：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">Echo</span><span class="token operator">.</span><span class="token keyword">private</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'App.Models.User.'</span> <span class="token operator">+</span> userId<span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">.</span><span class="token function">notification</span><span class="token punctuation">(</span><span class="token punctuation">(</span>notification<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span></span>
<span class="line">        console<span class="token operator">.</span><span class="token function">log</span><span class="token punctuation">(</span>notification<span class="token operator">.</span>type<span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="自定义通知渠道" tabindex="-1"><a class="header-anchor" href="#自定义通知渠道"><span>自定义通知渠道</span></a></h4>
<p>如果您想自定义实体的广播通知在哪个渠道上广播，您可以在可通知实体上定义一个 <code v-pre>receivesBroadcastNotificationsOn</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Models</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Broadcasting<span class="token punctuation">\</span>PrivateChannel</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Foundation<span class="token punctuation">\</span>Auth<span class="token punctuation">\</span>User</span> <span class="token keyword">as</span> Authenticatable<span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Notifiable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">User</span> <span class="token keyword">extends</span> <span class="token class-name">Authenticatable</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Notifiable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * The channels the user receives notification broadcasts on.</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">receivesBroadcastNotificationsOn</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">string</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token string single-quoted-string">'users.'</span><span class="token operator">.</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="短信通知" tabindex="-1"><a class="header-anchor" href="#短信通知"><span>短信通知</span></a></h2>
<h3 id="先决条件-2" tabindex="-1"><a class="header-anchor" href="#先决条件-2"><span>先决条件</span></a></h3>
<p>Laravel 中发送短信通知由 <a href="https://www.vonage.com/" target="_blank" rel="noopener noreferrer">Vonage</a> (以前称为 Nexmo)提供支持。在通过 Vonage 发送通知前，您需要安装 <code v-pre>laravel/vonage-notification-channel</code> 和 <code v-pre>guzzlehttp/guzzle</code> 扩展包：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">composer <span class="token keyword">require</span> laravel<span class="token operator">/</span>vonage<span class="token operator">-</span>notification<span class="token operator">-</span>channel guzzlehttp<span class="token operator">/</span>guzzle</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>这个扩展包包含一个 <a href="https://github.com/laravel/vonage-notification-channel/blob/3.x/config/vonage.php" target="_blank" rel="noopener noreferrer">配置文件</a>。但是，您不需要将此配置文件导出到您自己的应用程序。您只需要使用 <code v-pre>VONAGE_KEY</code> 和 <code v-pre>VONAGE_SECRET</code> 环境变量来定义您的 Vonage 公钥和密钥即可。</p>
<p>定义好密钥后，你可以设置一个 <code v-pre>VONAGE_SMS_FROM</code> 环境变量，该变量定义了你发送 SMS 消息的默认电话号码。你可以在 Vonage 控制面板中生成此电话号码：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token constant">VONAGE_SMS_FROM</span><span class="token operator">=</span><span class="token number">15556666666</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="格式化短信通知" tabindex="-1"><a class="header-anchor" href="#格式化短信通知"><span>格式化短信通知</span></a></h3>
<p>如果通知支持作为 SMS 发送，你应该在通知类上定义一个 <code v-pre>toVonage</code> 方法。此方法将接收一个 <code v-pre>$notifiable</code> 实体并应返回一个 <code v-pre>Illuminate\Notifications\Messages\VonageMessage</code> 实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Messages<span class="token punctuation">\</span>VonageMessage</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取通知的 Vonage / SMS 表达式。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toVonage</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">VonageMessage</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">VonageMessage</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">content</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Your SMS message content'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="unicode-内容" tabindex="-1"><a class="header-anchor" href="#unicode-内容"><span>Unicode 内容</span></a></h4>
<p>如果你的 SMS 消息将包含 unicode 字符，你应该在构造 <code v-pre>VonageMessage</code> 实例时调用 <code v-pre>unicode</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Messages<span class="token punctuation">\</span>VonageMessage</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取通知的 Vonage / SMS 表达式。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toVonage</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">VonageMessage</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">VonageMessage</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">content</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Your unicode message'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">unicode</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="自定义「来源」号码" tabindex="-1"><a class="header-anchor" href="#自定义「来源」号码"><span>自定义「来源」号码</span></a></h3>
<p>如果你想从一个不同于 <code v-pre>VONAGE_SMS_FROM</code> 环境变量所指定的电话号码发送通知，你可以在 <code v-pre>VonageMessage</code> 实例上调用 <code v-pre>from</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Messages<span class="token punctuation">\</span>VonageMessage</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取通知的 Vonage / SMS 表达式。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toVonage</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">VonageMessage</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">VonageMessage</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">content</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Your SMS message content'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">from</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'15554443333'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="添加客户关联" tabindex="-1"><a class="header-anchor" href="#添加客户关联"><span>添加客户关联</span></a></h3>
<p>如果你想跟踪每个用户、团队或客户的消费，你可以在通知中添加「客户关联」。Vonage 将允许你使用这个客户关联生成报告，以便你能更好地了解特定客户的短信使用情况。客户关联可以是任何字符串，最多 40 个字符。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Messages<span class="token punctuation">\</span>VonageMessage</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取通知的 Vonage / SMS 表达式。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toVonage</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">VonageMessage</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">VonageMessage</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">clientReference</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token keyword type-casting">string</span><span class="token punctuation">)</span> <span class="token variable">$notifiable</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">content</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Your SMS message content'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="路由短信通知" tabindex="-1"><a class="header-anchor" href="#路由短信通知"><span>路由短信通知</span></a></h3>
<p>要将 Vonage 通知路由到正确的电话号码，请在你的通知实体上定义 <code v-pre>routeNotificationForVonage</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Models</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Foundation<span class="token punctuation">\</span>Auth<span class="token punctuation">\</span>User</span> <span class="token keyword">as</span> Authenticatable<span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Notifiable</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Notification</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">User</span> <span class="token keyword">extends</span> <span class="token class-name">Authenticatable</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Notifiable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * Vonage 通道的路由通知。</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">routeNotificationForVonage</span><span class="token punctuation">(</span><span class="token class-name type-declaration">Notification</span> <span class="token variable">$notification</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">string</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">phone_number</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="slack-通知" tabindex="-1"><a class="header-anchor" href="#slack-通知"><span>Slack 通知</span></a></h2>
<h3 id="先决条件-3" tabindex="-1"><a class="header-anchor" href="#先决条件-3"><span>先决条件</span></a></h3>
<p>在你可以通过 Slack 发送通知之前，你必须通过 Composer 安装 Slack 通知通道：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line"><span class="token function">composer</span> require laravel/slack-notification-channel</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>此外，你还需要为你的 Slack 工作空间创建一个 <a href="https://api.slack.com/apps?new_app=1" target="_blank" rel="noopener noreferrer">Slack App</a>。</p>
<p>如果你只需要向同一个 Slack 工作空间发送通知，该工作空间是 App 创建的，你应该确保你的 App 具有 <code v-pre>chat:write</code>、<code v-pre>chat:write.public</code> 和 <code v-pre>chat:write.customize</code> 范围。这些范围可以从 Slack 的 「OAuth &amp; Permissions」 App 管理标签页中添加。</p>
<p>接下来，复制 App 的 「Bot User OAuth Token」 并将其放置在应用程序的 <code v-pre>services.php</code> 配置文件中的 <code v-pre>slack</code> 配置数组内。这个 Token 可以在 Slack 的 「OAuth &amp; Permissions」 标签页中找到：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'slack'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'notifications'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'bot_user_oauth_token'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'SLACK_BOT_USER_OAUTH_TOKEN'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'channel'</span> <span class="token operator">=></span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'SLACK_BOT_USER_DEFAULT_CHANNEL'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="应用分发" tabindex="-1"><a class="header-anchor" href="#应用分发"><span>应用分发</span></a></h4>
<p>如果你的应用程序需要向应用程序用户的外部 Slack 工作空间发送通知，你需要通过 Slack 「分发」你的 App 。App 分发可以从 Slack 中你的 App 的 「管理分发」标签页进行管理。一旦你的 App 被分发，你可以使用 <a href="https://learnku.com/docs/laravel/11.x/socialite" target="_blank" rel="noopener noreferrer">Socialite</a> 来代表你的应用程序的用户 <a href="https://learnku.com/docs/laravel/11.x/socialite#slack-bot-scopes" target="_blank" rel="noopener noreferrer">获取 Slack Bot 令牌</a>。</p>
<h3 id="格式化-slack-通知" tabindex="-1"><a class="header-anchor" href="#格式化-slack-通知"><span>格式化 Slack 通知</span></a></h3>
<p>你需要在你的通知类中定义一个 <code v-pre>toSlack</code> 方法。这个方法会接收一个 <code v-pre>$notifiable</code> 实体，并且应该返回一个 <code v-pre>Illuminate\Notifications\Slack\SlackMessage</code> 实例。通过这个实例，你可以使用 <a href="https://api.slack.com/block-kit" target="_blank" rel="noopener noreferrer">Slack 的 Block Kit API</a> 来构建丰富的通知内容。以下示例可以在 <a href="https://app.slack.com/block-kit-builder/T01KWS6K23Z#%7B%22blocks%22:%5B%7B%22type%22:%22header%22,%22text%22:%7B%22type%22:%22plain_text%22,%22text%22:%22Invoice%20Paid%22%7D%7D,%7B%22type%22:%22context%22,%22elements%22:%5B%7B%22type%22:%22plain_text%22,%22text%22:%22Customer%20%231234%22%7D%5D%7D,%7B%22type%22:%22section%22,%22text%22:%7B%22type%22:%22plain_text%22,%22text%22:%22An%20invoice%20has%20been%20paid.%22%7D,%22fields%22:%5B%7B%22type%22:%22mrkdwn%22,%22text%22:%22*Invoice%20No:*%5Cn1000%22%7D,%7B%22type%22:%22mrkdwn%22,%22text%22:%22*Invoice%20Recipient:*%5Cntaylor@laravel.com%22%7D%5D%7D,%7B%22type%22:%22divider%22%7D,%7B%22type%22:%22section%22,%22text%22:%7B%22type%22:%22plain_text%22,%22text%22:%22Congratulations!%22%7D%7D%5D%7D" target="_blank" rel="noopener noreferrer">Slack 的 Block Kit 构建器</a>中预览:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Slack<span class="token punctuation">\</span>BlockKit<span class="token punctuation">\</span>Blocks<span class="token punctuation">\</span>ContextBlock</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Slack<span class="token punctuation">\</span>BlockKit<span class="token punctuation">\</span>Blocks<span class="token punctuation">\</span>SectionBlock</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Slack<span class="token punctuation">\</span>BlockKit<span class="token punctuation">\</span>Composites<span class="token punctuation">\</span>ConfirmObject</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Slack<span class="token punctuation">\</span>SlackMessage</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取通知的 Slack 表达式。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toSlack</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">SlackMessage</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">SlackMessage</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">text</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'One of your invoices has been paid!'</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">headerBlock</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Invoice Paid'</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">contextBlock</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">ContextBlock</span> <span class="token variable">$block</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">                <span class="token variable">$block</span><span class="token operator">-></span><span class="token function">text</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Customer #1234'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">            <span class="token punctuation">}</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">sectionBlock</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">SectionBlock</span> <span class="token variable">$block</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">                <span class="token variable">$block</span><span class="token operator">-></span><span class="token function">text</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'An invoice has been paid.'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">                <span class="token variable">$block</span><span class="token operator">-></span><span class="token function">field</span><span class="token punctuation">(</span><span class="token string double-quoted-string">"*Invoice No:*\n1000"</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">markdown</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">                <span class="token variable">$block</span><span class="token operator">-></span><span class="token function">field</span><span class="token punctuation">(</span><span class="token string double-quoted-string">"*Invoice Recipient:*\ntaylor@laravel.com"</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">markdown</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">            <span class="token punctuation">}</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">dividerBlock</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">sectionBlock</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">SectionBlock</span> <span class="token variable">$block</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">                <span class="token variable">$block</span><span class="token operator">-></span><span class="token function">text</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Congratulations!'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">            <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="slack-交互性" tabindex="-1"><a class="header-anchor" href="#slack-交互性"><span>Slack 交互性</span></a></h3>
<p>Slack 的 Block Kit 通知系统提供了强大的功能来 <a href="https://api.slack.com/interactivity/handling" target="_blank" rel="noopener noreferrer">处理用户交互</a>。为了利用这些功能，你的 Slack 应用应该启用「交互性」并配置一个指向你的应用程序提供的URL的「请求URL」。这些设置可以在 Slack 内的「交互性与快捷方式」应用管理标签页中进行管理。</p>
<p>在以下示例中，使用了 <code v-pre>actionsBlock</code> 方法后，Slack 将向你的「请求 URL 」发送一个包含点击按钮的 Slack 用户、点击按钮的 ID 等信息的 <code v-pre>POST</code> 请求负载。然后，你的应用程序可以根据负载确定要采取的行动。你还应该 <a href="https://api.slack.com/authentication/verifying-requests-from-slack" target="_blank" rel="noopener noreferrer">验证请求</a> 确实是由 Slack 发出的。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Slack<span class="token punctuation">\</span>BlockKit<span class="token punctuation">\</span>Blocks<span class="token punctuation">\</span>ActionsBlock</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Slack<span class="token punctuation">\</span>BlockKit<span class="token punctuation">\</span>Blocks<span class="token punctuation">\</span>ContextBlock</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Slack<span class="token punctuation">\</span>BlockKit<span class="token punctuation">\</span>Blocks<span class="token punctuation">\</span>SectionBlock</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Slack<span class="token punctuation">\</span>SlackMessage</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取通知的 Slack 表示形式。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toSlack</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">SlackMessage</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">SlackMessage</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">text</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'One of your invoices has been paid!'</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">headerBlock</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Invoice Paid'</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">contextBlock</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">ContextBlock</span> <span class="token variable">$block</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">                <span class="token variable">$block</span><span class="token operator">-></span><span class="token function">text</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Customer #1234'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">            <span class="token punctuation">}</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">sectionBlock</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">SectionBlock</span> <span class="token variable">$block</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">                <span class="token variable">$block</span><span class="token operator">-></span><span class="token function">text</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'An invoice has been paid.'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">            <span class="token punctuation">}</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">actionsBlock</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">ActionsBlock</span> <span class="token variable">$block</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">                 <span class="token comment">// ID默认为 「button_acknowledgement _invoice」...</span></span>
<span class="line">                <span class="token variable">$block</span><span class="token operator">-></span><span class="token function">button</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Acknowledge Invoice'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">primary</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">                <span class="token comment">// 手动配置ID...</span></span>
<span class="line">                <span class="token variable">$block</span><span class="token operator">-></span><span class="token function">button</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Deny'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">danger</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">id</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'deny_invoice'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">            <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="确认模态框" tabindex="-1"><a class="header-anchor" href="#确认模态框"><span>确认模态框</span></a></h4>
<p>如果你希望用户在执行某个操作之前必须确认，你可以在定义按钮时调用 <code v-pre>confirm</code> 方法。<code v-pre>confirm</code> 方法接受一条消息和一个接收 <code v-pre>ConfirmObject</code> 实例的闭包：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Slack<span class="token punctuation">\</span>BlockKit<span class="token punctuation">\</span>Blocks<span class="token punctuation">\</span>ActionsBlock</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Slack<span class="token punctuation">\</span>BlockKit<span class="token punctuation">\</span>Blocks<span class="token punctuation">\</span>ContextBlock</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Slack<span class="token punctuation">\</span>BlockKit<span class="token punctuation">\</span>Blocks<span class="token punctuation">\</span>SectionBlock</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Slack<span class="token punctuation">\</span>BlockKit<span class="token punctuation">\</span>Composites<span class="token punctuation">\</span>ConfirmObject</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Slack<span class="token punctuation">\</span>SlackMessage</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取通知的 Slack 表现形式。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toSlack</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">SlackMessage</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">SlackMessage</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">text</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'One of your invoices has been paid!'</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">headerBlock</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Invoice Paid'</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">contextBlock</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">ContextBlock</span> <span class="token variable">$block</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">                <span class="token variable">$block</span><span class="token operator">-></span><span class="token function">text</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Customer #1234'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">            <span class="token punctuation">}</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">sectionBlock</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">SectionBlock</span> <span class="token variable">$block</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">                <span class="token variable">$block</span><span class="token operator">-></span><span class="token function">text</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'An invoice has been paid.'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">            <span class="token punctuation">}</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">actionsBlock</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">ActionsBlock</span> <span class="token variable">$block</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">                <span class="token variable">$block</span><span class="token operator">-></span><span class="token function">button</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Acknowledge Invoice'</span><span class="token punctuation">)</span></span>
<span class="line">                    <span class="token operator">-></span><span class="token function">primary</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">                    <span class="token operator">-></span><span class="token function">confirm</span><span class="token punctuation">(</span></span>
<span class="line">                        <span class="token string single-quoted-string">'Acknowledge the payment and send a thank you email?'</span><span class="token punctuation">,</span></span>
<span class="line">                        <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">ConfirmObject</span> <span class="token variable">$dialog</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">                            <span class="token variable">$dialog</span><span class="token operator">-></span><span class="token function">confirm</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Yes'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">                            <span class="token variable">$dialog</span><span class="token operator">-></span><span class="token function">deny</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'No'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">                        <span class="token punctuation">}</span></span>
<span class="line">                    <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">            <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="检查-slack-块" tabindex="-1"><a class="header-anchor" href="#检查-slack-块"><span>检查 Slack 块</span></a></h4>
<p>如果你想快速检查你构建的块，可以在 <code v-pre>SlackMessage</code> 实例上调用 <code v-pre>dd</code> 方法。<code v-pre>dd</code> 方法将生成并转储一个 URL 到 Slack 的 <a href="https://app.slack.com/block-kit-builder/" target="_blank" rel="noopener noreferrer">Block Kit 构建器</a>, 在浏览器中显示负载和通知的预览。你可以向 <code v-pre>dd</code> 方法传递 <code v-pre>true</code> 来转储原始负载：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">SlackMessage</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token operator">-></span><span class="token function">text</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'One of your invoices has been paid!'</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token operator">-></span><span class="token function">headerBlock</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Invoice Paid'</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token operator">-></span><span class="token function">dd</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="路由-slack-通知" tabindex="-1"><a class="header-anchor" href="#路由-slack-通知"><span>路由 Slack 通知</span></a></h3>
<p>要将Slack通知直接发送到适当的Slack团队和渠道，请在可通知模型上定义一个“routenotifationforslack”方法。这个方法可以返回以下三个值之一:</p>
<p>为了将 Slack 通知定向到适当的 Slack 团队和频道，你应该在可通知模型上定义一个 <code v-pre>routeNotificationForSlack</code> 方法。此方法可以返回以下三种值之一：</p>
<ul>
<li><code v-pre>null</code> - 这将延迟路由到通知本身配置的频道。在构建 <code v-pre>SlackMessage</code> 时，你可以使用 <code v-pre>to</code> 方法在通知内配置频道。</li>
<li>一个字符串，指定要发送通知的 Slack 频道，例如 <code v-pre>#support-channel</code>。</li>
<li>一个 <code v-pre>SlackRoute</code> 实例，允许你指定 OAuth 令牌和频道名称，例如 <code v-pre>SlackRoute::make($this-&gt;slack_channel, $this-&gt;slack_token)</code>。此方法应用于向外部工作空间发送通知。</li>
</ul>
<p>例如，从 <code v-pre>routeNotificationForSlack</code> 方法返回 <code v-pre>#support-channel</code> 将会将通知发送到与应用程序的 <code v-pre>services.php</code> 配置文件中找到的 Bot User OAuth 令牌关联的工作空间中的 <code v-pre>#support-channel</code> 频道：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Models</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Foundation<span class="token punctuation">\</span>Auth<span class="token punctuation">\</span>User</span> <span class="token keyword">as</span> Authenticatable<span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Notifiable</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Notification</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">User</span> <span class="token keyword">extends</span> <span class="token class-name">Authenticatable</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Notifiable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 路由 Slack 频道的通知.</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">routeNotificationForSlack</span><span class="token punctuation">(</span><span class="token class-name type-declaration">Notification</span> <span class="token variable">$notification</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">mixed</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token string single-quoted-string">'#support-channel'</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="通知外部-slack-工作空间" tabindex="-1"><a class="header-anchor" href="#通知外部-slack-工作空间"><span>通知外部 Slack 工作空间</span></a></h3>
<blockquote>
<p><strong>注意</strong><br>
在向外部 Slack 工作空间发送通知之前，您的 Slack 应用必须进行<a href="#slack-app-distribution">分发</a>。</p>
</blockquote>
<p>当然，你通常会希望将通知发送到你应用程序用户拥有的 Slack 工作空间。为此，首先你需要为用户获取一个 Slack OAuth 令牌。幸运的是，<a href="https://learnku.com/docs/laravel/11.x/socialite" target="_blank" rel="noopener noreferrer">Laravel Socialite</a> 包括一个 Slack 驱动，它将允许你轻松地使用 Slack 对你的应用程序用户进行认证，并<a href="https://learnku.com/docs/laravel/11.x/socialite#slack-bot-scopes" target="_blank" rel="noopener noreferrer">获得一个 bot 令牌</a>。</p>
<p>一旦你获得了机器人令牌并将其存储在应用程序的数据库中，你可以使用 <code v-pre>SlackRoute::make</code> 方法将通知路由到用户的工作空间。此外，你的应用程序可能需要为用户提供一个实际来指定应该发送通知到哪个频道：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Models</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Foundation<span class="token punctuation">\</span>Auth<span class="token punctuation">\</span>User</span> <span class="token keyword">as</span> Authenticatable<span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Notifiable</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Notification</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Slack<span class="token punctuation">\</span>SlackRoute</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">User</span> <span class="token keyword">extends</span> <span class="token class-name">Authenticatable</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Notifiable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 将通知路由到 Slack 频道。</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">routeNotificationForSlack</span><span class="token punctuation">(</span><span class="token class-name type-declaration">Notification</span> <span class="token variable">$notification</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">mixed</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token class-name static-context">SlackRoute</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token property">slack_channel</span><span class="token punctuation">,</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">slack_token</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="本地化通知" tabindex="-1"><a class="header-anchor" href="#本地化通知"><span>本地化通知</span></a></h2>
<p>Laravel 允许你在除了当前请求语言环境之外的其他语言环境中发送通知，甚至在通知被队列化的情况下也能记住此语言环境。</p>
<p>为了实现这一功能，<code v-pre>Illuminate\Notifications\Notification</code> 类提供了 <code v-pre>locale</code> 方法来设置所需的语言环境。在通知被评估时，应用程序将切换到此语言环境，然后在评估完成后恢复到以前的语言环境：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$user</span><span class="token operator">-></span><span class="token function">notify</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">InvoicePaid</span><span class="token punctuation">(</span><span class="token variable">$invoice</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">locale</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'es'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>通过 <code v-pre>Notification</code> 门面，也可以实现多个通知实体的本地化：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Notification</span><span class="token operator">::</span><span class="token function">locale</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'es'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">send</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token variable">$users</span><span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token class-name">InvoicePaid</span><span class="token punctuation">(</span><span class="token variable">$invoice</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="用户首选语言环境" tabindex="-1"><a class="header-anchor" href="#用户首选语言环境"><span>用户首选语言环境</span></a></h3>
<p>有时，应用程序会存储每个用户的首选区域设置。通过在你的可通知模型上实现 <code v-pre>HasLocalePreference</code> 合同，你可以指示 Laravel 在发送通知时使用此存储的区域设置：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Contracts<span class="token punctuation">\</span>Translation<span class="token punctuation">\</span>HasLocalePreference</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">User</span> <span class="token keyword">extends</span> <span class="token class-name">Model</span> <span class="token keyword">implements</span> <span class="token class-name">HasLocalePreference</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 获取用户的首选语言环境。</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">preferredLocale</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">string</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">locale</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>一旦你实现了这个接口，当发送通知和邮件到该模型时，Laravel 会自动使用首选语言环境。因此，在使用此接口时不需要调用 <code v-pre>locale</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$user</span><span class="token operator">-></span><span class="token function">notify</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">InvoicePaid</span><span class="token punctuation">(</span><span class="token variable">$invoice</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="测试" tabindex="-1"><a class="header-anchor" href="#测试"><span>测试</span></a></h2>
<p>你可以使用 <code v-pre>Notification</code> 门面的 <code v-pre>fake</code> 方法来阻止通知被发送。通常情况下，发送通知与你实际测试的代码无关。很可能，只需要断言 Laravel 被指示发送了给定的通知即可。</p>
<p>在调用 <code v-pre>Notification</code> 门面的 <code v-pre>fake</code> 方法后，你可以断言已经被告知将通知发送给用户，甚至检查通知接收到的数据：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>OrderShipped</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Notification</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">test</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'orders can be shipped'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">Notification</span><span class="token operator">::</span><span class="token function">fake</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 执行订单发货...</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 断言没有发送通知...</span></span>
<span class="line">    <span class="token class-name static-context">Notification</span><span class="token operator">::</span><span class="token function">assertNothingSent</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 断言通知已发送给给定用户...</span></span>
<span class="line">    <span class="token class-name static-context">Notification</span><span class="token operator">::</span><span class="token function">assertSentTo</span><span class="token punctuation">(</span></span>
<span class="line">        <span class="token punctuation">[</span><span class="token variable">$user</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token class-name static-context">OrderShipped</span><span class="token operator">::</span><span class="token keyword">class</span></span>
<span class="line">    <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 断言未发送通知...</span></span>
<span class="line">    <span class="token class-name static-context">Notification</span><span class="token operator">::</span><span class="token function">assertNotSentTo</span><span class="token punctuation">(</span></span>
<span class="line">        <span class="token punctuation">[</span><span class="token variable">$user</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token class-name static-context">AnotherNotification</span><span class="token operator">::</span><span class="token keyword">class</span></span>
<span class="line">    <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 断言已发送给定数量的通知...</span></span>
<span class="line">    <span class="token class-name static-context">Notification</span><span class="token operator">::</span><span class="token function">assertCount</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">Tests<span class="token punctuation">\</span>Feature</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>OrderShipped</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Notification</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Tests<span class="token punctuation">\</span>TestCase</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">ExampleTest</span> <span class="token keyword">extends</span> <span class="token class-name">TestCase</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">test_orders_can_be_shipped</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token class-name static-context">Notification</span><span class="token operator">::</span><span class="token function">fake</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">       <span class="token comment">// 执行订单发货...</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// 断言没有发送通知...</span></span>
<span class="line">        <span class="token class-name static-context">Notification</span><span class="token operator">::</span><span class="token function">assertNothingSent</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// 断言通知已发送给给定用户...</span></span>
<span class="line">        <span class="token class-name static-context">Notification</span><span class="token operator">::</span><span class="token function">assertSentTo</span><span class="token punctuation">(</span></span>
<span class="line">            <span class="token punctuation">[</span><span class="token variable">$user</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token class-name static-context">OrderShipped</span><span class="token operator">::</span><span class="token keyword">class</span></span>
<span class="line">        <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// 断言未发送通知...</span></span>
<span class="line">        <span class="token class-name static-context">Notification</span><span class="token operator">::</span><span class="token function">assertNotSentTo</span><span class="token punctuation">(</span></span>
<span class="line">            <span class="token punctuation">[</span><span class="token variable">$user</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token class-name static-context">AnotherNotification</span><span class="token operator">::</span><span class="token keyword">class</span></span>
<span class="line">        <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// 断言已发送给定数量的通知...</span></span>
<span class="line">        <span class="token class-name static-context">Notification</span><span class="token operator">::</span><span class="token function">assertCount</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以通过向 <code v-pre>assertSentTo</code> 或 <code v-pre>assertNotSentTo</code> 方法传递一个闭包来断言发送了符合给定「真实性测试」的通知。如果发送了至少一个符合给定真实性测试的通知，则断言将成功：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Notification</span><span class="token operator">::</span><span class="token function">assertSentTo</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token variable">$user</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">OrderShipped</span> <span class="token variable">$notification</span><span class="token punctuation">,</span> <span class="token keyword type-hint">array</span> <span class="token variable">$channels</span><span class="token punctuation">)</span> <span class="token keyword">use</span> <span class="token punctuation">(</span><span class="token variable">$order</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$notification</span><span class="token operator">-></span><span class="token property">order</span><span class="token operator">-></span><span class="token property">id</span> <span class="token operator">===</span> <span class="token variable">$order</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="按需通知-1" tabindex="-1"><a class="header-anchor" href="#按需通知-1"><span>按需通知</span></a></h4>
<p>如果你正在测试的代码发送 <a href="https://chat.openai.com/chat#on-demand-notifications" target="_blank" rel="noopener noreferrer">即时通知</a>，你可以使用 <code v-pre>assertSentOnDemand</code> 方法测试是否发送了即时通知：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Notification</span><span class="token operator">::</span><span class="token function">assertSentOnDemand</span><span class="token punctuation">(</span><span class="token class-name static-context">OrderShipped</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>通过将闭包作为 <code v-pre>assertSentOnDemand</code> 方法的第二个参数传递，你可以确定是否将即时通知发送到了正确的 「route」 地址：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Notification</span><span class="token operator">::</span><span class="token function">assertSentOnDemand</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token class-name static-context">OrderShipped</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">OrderShipped</span> <span class="token variable">$notification</span><span class="token punctuation">,</span> <span class="token keyword type-hint">array</span> <span class="token variable">$channels</span><span class="token punctuation">,</span> <span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span> <span class="token keyword">use</span> <span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$notifiable</span><span class="token operator">-></span><span class="token property">routes</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'mail'</span><span class="token punctuation">]</span> <span class="token operator">===</span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token property">email</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="通知事件" tabindex="-1"><a class="header-anchor" href="#通知事件"><span>通知事件</span></a></h2>
<h4 id="通知发送事件" tabindex="-1"><a class="header-anchor" href="#通知发送事件"><span>通知发送事件</span></a></h4>
<p>发送通知时，通知系统会调度 <code v-pre>Illuminate\Notifications\Events\NotificationSending</code> 事件。 这包含「可通知」实体和通知实例本身。 你可以在应用程序的 EventServiceProvider 中为该事件注册 <a href="https://learnku.com/docs/laravel/11.x/events" target="_blank" rel="noopener noreferrer">监听器</a>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Events<span class="token punctuation">\</span>NotificationSending</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">CheckNotificationStatus</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 处理给定的事件。</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">handle</span><span class="token punctuation">(</span><span class="token class-name type-declaration">NotificationSending</span> <span class="token variable">$event</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token comment">// ...</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果 <code v-pre>NotificationSending</code> 事件的事件监听器从其 <code v-pre>handle</code> 方法返回 <code v-pre>false</code> ，则不会发送通知：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 处理给定的事件。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">handle</span><span class="token punctuation">(</span><span class="token class-name type-declaration">NotificationSending</span> <span class="token variable">$event</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">bool</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token constant boolean">false</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>&quot;在事件监听器中，你可以访问事件的 <code v-pre>notifiable</code> 、<code v-pre>notification</code> 和 <code v-pre>channel</code> 属性，以获取更多关于通知接收者或通知本身的信息：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 处理给定的事件。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">handle</span><span class="token punctuation">(</span><span class="token class-name type-declaration">NotificationSending</span> <span class="token variable">$event</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// $event->channel</span></span>
<span class="line">    <span class="token comment">// $event->notifiable</span></span>
<span class="line">    <span class="token comment">// $event->notification</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="发送通知事件" tabindex="-1"><a class="header-anchor" href="#发送通知事件"><span>发送通知事件</span></a></h4>
<p>发送通知时，通知系统会调度 <code v-pre>Illuminate\Notifications\Events\NotificationSent</code> <a href="https://learnku.com/docs/laravel/11.x/events" target="_blank" rel="noopener noreferrer">事件</a>。该事件包含 “被通知” 实体和通知实例本身。你可以在应用程序中为该事件创建 <a href="https://learnku.com/docs/laravel/11.x/events" target="_blank" rel="noopener noreferrer">事件侦听器</a>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Events<span class="token punctuation">\</span>NotificationSent</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">LogNotification</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 处理给定事件。</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">handle</span><span class="token punctuation">(</span><span class="token class-name type-declaration">NotificationSent</span> <span class="token variable">$event</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token comment">// ...</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>在事件监听器中，您可以访问事件的<code v-pre>notifiable</code>, <code v-pre>notification</code>, <code v-pre>channel</code>, 和<code v-pre>response</code> 属性，以了解有关通知接收者或通知本身的更多信息：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 处理给定事件。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">handle</span><span class="token punctuation">(</span><span class="token class-name type-declaration">NotificationSent</span> <span class="token variable">$event</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// $event->channel</span></span>
<span class="line">    <span class="token comment">// $event->notifiable</span></span>
<span class="line">    <span class="token comment">// $event->notification</span></span>
<span class="line">    <span class="token comment">// $event->response</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="自定义渠道" tabindex="-1"><a class="header-anchor" href="#自定义渠道"><span>自定义渠道</span></a></h2>
<p>Laravel 提供了一些通知渠道，也可以编写自己的驱动程序，通过其他渠道发送通知。<br>
首先，定义一个包含 <code v-pre>send</code> 方法的类。方法应该接收两个参数：<code v-pre>$notifiable</code> 和 <code v-pre>$notification</code>。</p>
<p>在 <code v-pre>send</code> 方法中， 您可以调用通知上的方法来获取渠道可理解的消息对象，然后将通知发送到 <code v-pre>$notifiable</code> 实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Notifications</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Notification</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">VoiceChannel</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 发送指定通知。</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">send</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">,</span> <span class="token class-name type-declaration">Notification</span> <span class="token variable">$notification</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$message</span> <span class="token operator">=</span> <span class="token variable">$notification</span><span class="token operator">-></span><span class="token function">toVoice</span><span class="token punctuation">(</span><span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// 向 $notifiable 实例发送通知...</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>一旦定义了通知通道类，就可以从任何通知的 <code v-pre>via</code> 方法中返回类名。在这个例子中，通知的 <code v-pre>toVoice</code> 方法可以返回任何你选择用来表示语音信息的对象。例如，您可以定义自己的 <code v-pre>VoiceMessage</code> 类来表示这些信息：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Notifications</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Messages<span class="token punctuation">\</span>VoiceMessage</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>VoiceChannel</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Bus<span class="token punctuation">\</span>Queueable</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Contracts<span class="token punctuation">\</span>Queue<span class="token punctuation">\</span>ShouldQueue</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Notification</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">InvoicePaid</span> <span class="token keyword">extends</span> <span class="token class-name">Notification</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">use</span> <span class="token package">Queueable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 获取通知渠道。</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">via</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">string</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token class-name static-context">VoiceChannel</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 获取通知语音。</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">toVoice</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$notifiable</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">VoiceMessage</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token comment">// ...</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/11.x/notificationsmd/16683" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/11.x/no...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/11.x/notificationsmd/16683" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/11.x/no...</a></p>
</blockquote>
</div></template>


